
$(function () {
    $('.datetimepicker').datetimepicker({
        icons: {
            time: "fa fa-clock-o",
            date: "fa fa-calendar",
            up: "fa fa-chevron-up",
            down: "fa fa-chevron-down",
            previous: 'fa fa-chevron-left',
            next: 'fa fa-chevron-right',
            today: 'fa fa-screenshot',
            clear: 'fa fa-trash',
            close: 'fa fa-remove'
        },
        format: 'YYYY-MM-DD',
    });
})
$('#event_time').val(dateToYMD(new Date()));

findAllEvent();

var event_list;
var page_count = 15;

/**
 * 搜索比赛
 */
function searchAction() {
    findAllEvent();
}
/**
 * 查询今日所有比赛
 */
function findAllEvent() {
    var date = $('#event_time').val();
    var status = $('#status_select').val();
    var keyword = $('#keyword').val();

    var data = {};
    if (null == date && data.length == 0){
        showNotification('bottom', 'right', '请选择开始日期');
        return;
    }

    if (null != status){
        data['state'] = status;
    }
    if (null != keyword && keyword.length != 0){
        data['keyword'] = keyword;
    }

    data['date'] = date;

    $.ajax({
        url: baseUrl.base + 'country/eventList',
        type: 'get',
        data: data,
        success: function (data) {
            var code = data.code;

            if (code == '0') {
                //创建成功

                var res = data.results;

                event_list = res;

                cutPagination(1);

                // 配置分页
                $("#p1").pagination({
                    totalData: res.length,
                    showData:page_count,
                    callback: function (idx) {
                        cutPagination(idx);
                    }
                });
            }
        }
    });
}

/**
 * 分页
 * @param page 从1开始
 */
function cutPagination(page) {

    var start = (page - 1) * page_count;
    var end = start + page_count;

    if (end > event_list.length){
        end = event_list.length;
    }

    $("#tbody").empty();

    $("#table tbody").html("");

    for (var i = start; i < end; i ++){
        var event = event_list[i];

        var tr = document.createElement("tr");

        var td = document.createElement("td");
        var id = document.createTextNode(event.eventId);
        td.appendChild(id)
        tr.appendChild(td);

        var td = document.createElement("td");
        var name = document.createTextNode(event.homeTeamName +' VS ' + event.awayTeamName);
        td.appendChild(name)
        tr.appendChild(td);

        var td = document.createElement("td");
        var name = document.createTextNode(event.competitionName);
        td.appendChild(name)
        tr.appendChild(td);

        var td = document.createElement("td");
        var name = document.createTextNode(timestampToTime(event.eventTime / 10000));
        td.appendChild(name)
        tr.appendChild(td);

        var td = document.createElement("td");
        var state = "";
        if (event.state == 0){
            state = "未开赛";
        }else if (event.state == 1){
            state = "已结束";
        }else if (event.state == 2){
            state = "数据取消";
        }else if (event.state == 3){
            state = "正在进行";
        }else if (event.state == 4){
            state = "延期";
        }else if (event.state == 5){
            state = "中途废弃";
        }

        var name = document.createTextNode(state);
        td.appendChild(name)
        tr.appendChild(td);

        var td = document.createElement("td");
        var btn1 = document.createElement("button");
        btn1.setAttribute("type", "button");
        btn1.setAttribute("class", "btn btn-success btn-sm");
        btn1.setAttribute("event_id", event.eventId);
        // btn1.setAttribute("href","");

        btn1.onclick = function () {

            $(location).attr('href', baseUrl.base + 's/market-manager.do?eventId=' +this.getAttribute("event_id"));
        }

        var tdContent = document.createTextNode("市场");
        btn1.appendChild(tdContent);
        td.appendChild(btn1);

        var btn2 = document.createElement("button");
        btn2.setAttribute("type", "button");
        btn2.setAttribute("event_id", event.eventId);
        btn2.setAttribute("home_name", event.homeTeamName);
        btn2.setAttribute("away_name", event.awayTeamName);
        btn2.setAttribute("event_time", timestampToTime(event.eventTime / 10000));

        if (event.enabled == 1) {
            //     已禁用
            btn2.setAttribute("class", "btn btn-danger btn-sm");
            var tdContent = document.createTextNode("已禁用");
            btn2.setAttribute("status", 0);
            btn2.onclick = function (ev) {
                setDisabledAction(this);
            }
        } else {
            //已启用
            btn2.setAttribute("class", "btn btn-info btn-sm");
            var tdContent = document.createTextNode("已启用");
            btn2.setAttribute("status", 1);
            btn2.onclick = function (ev) {
                setDisabledAction(this);
            }
        }

        btn2.appendChild(tdContent);
        td.appendChild(btn2);
        tr.appendChild(td);

        document.getElementById("tbody").appendChild(tr);
        document.getElementById("tbody").innerHTML;

    }

}

/**
 * 开启禁用比赛
 */
function setDisabledAction(element) {

    var event_id = element.getAttribute("event_id");
    var status = element.getAttribute("status");
    var home_name = element.getAttribute("home_name");
    var away_name = element.getAttribute("away_name");
    var event_time = element.getAttribute("event_time");

    var data = {
        "eventId":event_id,
        "isEnable":status,
        "homeName":home_name,
        "awayName":away_name,
        "eventTime":event_time
    }

    $.ajax({
        url: baseUrl.base + '/customEvent/setEvent',
        type: 'get',
        data: data,
        success: function (data) {

            if (data.code == '0') {
                //创建成功

                if (element.getAttribute("status") == 1) {
                    //已禁用变为已启用
                    element.setAttribute("class", "btn btn-danger btn-sm");
                    element.setAttribute("status",0);
                    element.onclick = function (ev) {
                        setDisabledAction(this);
                    }
                    element.innerText = "已禁用";
                    showNotification('bottom', 'right', '禁用成功')
                } else {
                    ////已启用变为禁用
                    element.setAttribute("class", "btn btn-info btn-sm");
                    element.setAttribute("status",1);
                    element.onclick = function (ev) {
                        setDisabledAction(this);
                    }
                    element.innerText = "已启用";
                    showNotification('bottom', 'right', '已启成功')
                }

            }
        }
    });
}
